package com.ktgj.hotel.service.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.redis.service.RedisService;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.BankCard;
import com.ktgj.customer.api.domain.Card;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.hotel.domain.*;
import com.ktgj.hotel.domain.elong.ValidateCreditCardCondition;
import com.ktgj.hotel.domain.rooms.ValidateCreditCardResult;
import com.ktgj.hotel.domain.vo.*;
import com.ktgj.hotel.mapper.*;
import com.ktgj.hotel.service.IHotelService;
import com.ktgj.hotel.utils.CheckBankCard;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.hotel.service.IHotelOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@Service
public class HotelOrderServiceImpl implements IHotelOrderService 
{

    private static String HOTEL = "hotel:";

    private static String ORDER = "order:";

    private static String notifyUrl = "http://39.106.54.2:8080/WXPay/orderResult";

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

//    @Autowired
//    private RedisService redisService;
//
    @Autowired
    private RemoteCustomerService remoteCustomerService;
//
//    @Autowired
//    private IHotelService hotelService;

    @Autowired
    private HotelOrderStatusMapper hotelOrderStatusMapper;

    @Autowired
    private HotelOrderRoomMapper hotelOrderRoomMapper;

    @Autowired
    private HotelOrderFlowstatusMapper hotelOrderFlowstatusMapper;

    @Autowired
    private HotelOrderRefundapplyMapper hotelOrderRefundapplyMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param orderid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public HotelOrder selectHotelOrderById(Long orderid)
    {
        return hotelOrderMapper.selectHotelOrderById(orderid);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param hotelOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HotelOrder> selectHotelOrderList(HotelOrder hotelOrder)
    {
        return hotelOrderMapper.selectHotelOrderList(hotelOrder);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param hotelOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHotelOrder(HotelOrder hotelOrder)
    {
        return hotelOrderMapper.insertHotelOrder(hotelOrder);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param hotelOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHotelOrder(HotelOrder hotelOrder)
    {
        return hotelOrderMapper.updateHotelOrder(hotelOrder);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param orderids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteHotelOrderByIds(Long[] orderids)
    {
        return hotelOrderMapper.deleteHotelOrderByIds(orderids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param orderid 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteHotelOrderById(Long orderid)
    {
        return hotelOrderMapper.deleteHotelOrderById(orderid);
    }

    /**
     * 修改订单状态
     * */
    @Override
    public int updateHotelOrderState(HotelOrder hotelOrder) {
        int i = hotelOrderMapper.updateHotelOrderState(hotelOrder);
        return i;
    }

    /**
     * 查询酒店订单量
     * */
    @Override
    public List<HotelOrder> selectHotelOrderNum(Long customerId) {
        List<HotelOrder> hotelOrders = hotelOrderMapper.selectHotelOrderNum(customerId);
        return hotelOrders;
    }

    /**
     * 酒店订单
     * */
    @Override
    public HotelDetailsOrderVo orderDetails(Long orderId) {
        HotelDetailsOrderVo hotelDetailsOrderVo = new HotelDetailsOrderVo();
        HotelOrder hotelOrder = hotelOrderMapper.checkHotelReservations(orderId);
        return null;
    }

    @Override
    public int queryHotelOrderNumber(Long customerId) {
        return hotelOrderMapper.queryHotelOrderNumber(customerId);
    }

    /**
     * 行程酒店订单
     * */
    @Override
    public JourneyHotelVo checkHotel(Long orderId) {
        HotelOrder hotelOrder = hotelOrderMapper.checkHotel(orderId);
        JourneyHotelVo journeyHotelVo = new JourneyHotelVo();
        journeyHotelVo.setOrderId(hotelOrder.getOrderid().toString());
        journeyHotelVo.setBusinessType(EmHelper.businessType.HOTEL.getValue());
        journeyHotelVo.setActualPriceTotal(hotelOrder.getPayAmount().toString());
        HotelOrderStatus hotelOrderStatus = hotelOrderStatusMapper.selectHotelOrderStatusById(hotelOrder.getOrderstatusid());
        journeyHotelVo.setOrderStatusID(hotelOrderStatus.getOrderstatusname());
        journeyHotelVo.setHotelName(hotelOrder.getHotelname());
        journeyHotelVo.setNumberOfRooms(Long.valueOf(hotelOrder.getNumberOfRooms()));
        journeyHotelVo.setArriveTime(hotelOrder.getArrivetime());
        journeyHotelVo.setCheckOutTime(hotelOrder.getCheckouttime());
        return journeyHotelVo;
    }

    /**
     * 酒店订单退单详情
     * */
    @Override
    public HotelRetreatVo hotelRetreat(Long id) throws Exception {
        HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderById(id);
        if (hotelOrder != null){
            HotelRetreatVo hotelRetreatVo = new HotelRetreatVo();
            hotelRetreatVo.setOrderNum(hotelOrder.getOrderNum());
            if (hotelOrder.getELongOrderId() != null){
                hotelRetreatVo.seteLongOrderNum(hotelOrder.getELongOrderId().toString());
            }
            hotelRetreatVo.setRetreatAmount(hotelOrder.getPayAmount().toString());

            BigDecimal totalRoomCharge;
            if (hotelOrder.getDepositAmount() != null && hotelOrder.getDepositAmount().compareTo(new BigDecimal(0)) != 0){
                hotelRetreatVo.setReserveMoney(hotelOrder.getDepositAmount().toString());
                totalRoomCharge = hotelOrder.getPayAmount().add(hotelOrder.getDepositAmount());
            }
            else {
                hotelRetreatVo.setReserveMoney("0");
                totalRoomCharge = hotelOrder.getPayAmount();
            }

            hotelRetreatVo.setTotalRoomCharge(totalRoomCharge.toString());
            return hotelRetreatVo;
        }
        else {
            throw new Exception("无效的订单id");
        }
    }

    /**
     * 酒店取消预定订单
     * */
    @Override
    public List<HotelReturnVo> hotelCancelOrderList(HotelCancelOrderVo hotelCancelOrderVo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<HotelReturnVo> hotelReturnVoList = new ArrayList<>();
        List<HotelOrder> hotelOrders = new ArrayList<>();
        List<HotelOrderRefundapply> hotelOrderRefundapplies = hotelOrderRefundapplyMapper.hotelOrderRefundapplyList();
        if (hotelOrderRefundapplies.size() > 0){
            for (HotelOrderRefundapply hotelOrderRefundapply : hotelOrderRefundapplies) {
                HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderByOrderNum(hotelOrderRefundapply.getOrderid().toString());
                hotelOrders.add(hotelOrder);
            }
        }
        if (hotelOrders.size() > 0){
            for (HotelOrder hotelOrder : hotelOrders) {
                if (hotelOrder != null){
                    HotelReturnVo hotelReturnVo = new HotelReturnVo();
                    hotelReturnVo.setOrderNum(hotelOrder.getOrderNum());
                    hotelReturnVo.setHotelName(hotelOrder.getHotelname());

                    List<HotelOrderRoom> hotelOrderRoomList = hotelOrderRoomMapper.selectHotelOrderRoom(Long.valueOf(hotelOrder.getOrderNum()));

                    String name = null;
                    if (hotelOrderRoomList.size() > 0){
                        for (int i = 0 ; i < hotelOrderRoomList.size() ; i++) {
                            if (i == 0){
                                name = hotelOrderRoomList.get(i).getGuestname();
                                hotelReturnVo.setContact(hotelOrderRoomList.get(i).getGuestname());
                            }
                            else {
                                name = name + "," + hotelOrderRoomList.get(i).getGuestname();
                            }
                        }
                    }
                    hotelReturnVo.setName(name);
                    hotelReturnVo.setReservation(hotelOrder.getEmployeename());

                    HotelOrderFlowstatus hotelOrderFlowstatus = hotelOrderFlowstatusMapper.selectHotelOrderFlowstatusById(hotelOrder.getOrderflowstatusid());
                    if (hotelOrderFlowstatus != null){
                        hotelReturnVo.setOrderProcessStart(hotelOrderFlowstatus.getOrderflowname());
                    }

                    HotelOrderStatus hotelOrderStatus = hotelOrderStatusMapper.selectHotelOrderStatusById(hotelOrder.getOrderstatusid());
                    if (hotelOrderStatus != null){
                        hotelReturnVo.setOrderStart(hotelOrderStatus.getOrderstatusname());
                    }

                    hotelReturnVo.setPaymentMethod(hotelOrder.getOrdertype().toString());

                    hotelReturnVo.setCheckTime(hotelOrder.getArrivetime());
                    hotelReturnVo.setCheckOutTime(hotelOrder.getCheckouttime());
                    if (hotelOrder.getCreateTime() != null){
                        hotelReturnVo.setScheduledTime(format.format(hotelOrder.getCreateTime()));
                    }
                    hotelReturnVoList.add(hotelReturnVo);
                }
            }
        }
        return hotelReturnVoList;
    }

    /**
     * 订单号获取酒店订单信息
     * */
    @Override
    public HotelOrder orderNum(String orderNum) {
        HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderByOrderNum(orderNum);
        return hotelOrder;
    }

    @Override
    public List<HotelOrder> orderListThings()
    {
        return hotelOrderMapper.getOrderListThings();
    }
}
